;_________________________________________________________________________________
;*********************************************************************************
;	Title: Version Updater
;
;	Group: Overview
;
;		Writes the current version number of compiled PM App Launcher files to an INI file.
;		The root folder in which to find the EXE files and the output INI file can
;		be customized by use of command line switches (see the 'Using Switches' section).
;
;		This file is used mainly upon compiling the PM Application Launcher project in order
;		to capture each app's latest version for use in checking for updates, performed in
;		the <PML_Updater> module. See the 'Golbal Variables' section for more info on default
;		settings.
;---------------------------------------------------------------------------------------------
; 	Group: How to Use
;
;		Standard (Default) Use:
;		
;		If *no parameters are passed* to the script, it will use the script default values 
;		to determine the root directory of the EXE files and the output path/filename of the
;		INI file. See the 'Global Variables' section for info on script defaults.
;
;		Using Switches:
;
;		Command line switches are commands passed to the script (as parameters) when it is run.
;		Switches are denoted by either a dash (-) or a slash (/) as the first character of the
;		parameter; see the example below-
;
;		> Run, Version Updater.ahk -switch1 /switch2
;
;		Switches may (but are not required to) have a corresponding value, which would be passed 
;		as the parameter immediately following the switch; if the value contains spaces, it must be 
;		in quotes. See the example below-
;
;		> Run, Version Updater.ahk -switch1 "Switch1 value" -switch2 105
;---------------------------------------------------------------------------------------------
;    Group: Command Line Switches
;
;        A list of the switches accepted by this script
;
;		installed		-	Gets the version numbers of the files
;						installed in my documents folder instead
;						of the source exe files
;		out {Path}		-	Outputs the versioncheck.ini file to the
;						given location instead of in epub\pm app
;						launcher folder
;		name {fname}	-	Saves the output ini file as {fname}.ini instead
;						of versioncheck.ini. If "time" is passed for {fname}
;						then the file will be named with the current time-
;						stamp showing the date and time (this can be useful
;						when doing somehting like trying to capture/save the
;						file versions at a given time)
;---------------------------------------------------------------------------------------------
;	Group: Remarks
;
;		- In order to run properly (when used as described in the Standard Use section above) this file
;		  must be located in the \Include folder within the PM App Launcher source root folder.
;		
;		- If the -installed switch is on, then the working dir for this file must be
;		  the \Include directory within the Installed PM App Launcher folder;
;		  (usually-- [user folder]\My Documents\PM App Launcher).
;_________________________________________________________________________________
;---------------------------------------------------------------------------------
 #NoEnv
 #SingleInstance, Force
 SetWorkingDir %A_ScriptDir%
 SetTitleMatchMode, 2
 SendMode, Input
 Functions()
;_________________________________________________________________________________
;*********************************************************************************




;========================================================================================================
;	Group: Global Variables
;		List of the customizable global variables in the script
;		the values are easily changed, found at the top of the script's code.
;
;	ROOT_PATH		- Full path to the PM App Launcher root folder (preferably relative using script dir)
;	DEFAULT_OUTPATH - Full path of the folder to use as default for saving the output INI file
;	INI_FILENAME 	- Filename (including the .ini extension) to use as the default output ini filename.
;_________________________________________________________________________________________________________
;=========================================================================================================
	
	;___________________________________________________________
	; 		FULL PATH OF PM APP LAUNCHER SCRIPT FILE ROOT FOLDER
	;
	 ROOT_PATH			= %A_ScriptDir%\..
	
	
	;________________________________________________________________
	; 		FULL PATH OF FOLDER IN WHICH TO SAVE THE VERSION INI FILE
	;
	 DEFAULT_OUTPATH	= \\corp.halliburton.com\team\WD\Business Development And Technology\General\Engineering Public\PM App Launcher\App Data\Internal
	
	;_____________________________________________________________________
	; 		FILE NAME TO SAVE THE OUTPUT INI FILE AS (MUST INCLUDE ".INI")
	;
	 INI_FILENAME 		= versionCheck.ini






;======================================================================
;                          HANDLE PARAMETERS
;======================================================================
	if (%0% > 0){
		loop %0%
			paramList .= (paramList ? " " : "") %a_index%
		p := param(paramList)
	}

	
	;________________________
	;	 	-INSTALLED OPTION
	;
		SetWorkingDir, A_MyDocuments "\PM App Launcher\Include" : A_ScriptDir
	
	
	
	;__________________
	; 	    -OUT OPTION
	;
		OUTPATH := p.out ? p.out : DEFAULT_OUTPATH


	
	;___________________
	; 	    -NAME OPTION
	;	
		INI_FILENAME := p.name ? (p.name = "time" ? FormatTime(a_now, "yyyy-M-d  (H.m tt)") ".ini" : p.name) : INI_FILENAME


	;___________________________
	;	 	FINALIZE OUTPUT FILE
	;
		OUT_FILE := trim(OUTPATH) "\" trim(INI_FILENAME)





;===========================================================================
;                WRITE FILE VERSIONS TO OUTPUT INI FILE
;===========================================================================
	
	
	;_____________________________________________________________
	; 	WRITE THE FILE VERSIONS TO THE INI UNLESS THEY DON'T EXIST
	;	
		;-- PM APP LAUNCHER --
		;=====================
		If FileGetVersion(ROOT_PATH "\PM Application Launcher.exe")		
			IniWrite, % FileGetVersion(ROOT_PATH "\PM Application Launcher.exe"), %OUT_FILE%, PM App Launcher, Version
		else {
			MsgBox, Could not find compiled PM Application Launcher.exe`n`n Aborting....
			ExitApp
		}
		Sleep 50
		
		;-- CWI SB --
		;============
		if FileGetVersion(A_ScriptDir "\CWI SB\CWI Search Bar.exe")
			IniWrite, % FileGetVersion(A_ScriptDir "\CWI SB\CWI Search Bar.exe"), %OUT_FILE%, CWI Search Bar, Version
		else {
			MsgBox, Could not find compiled CWI Search Bar.exe`n`n Aborting....
			ExitApp
		}
		Sleep 50
				
		;-- CWI SETTINGS --
		;==================	
		if FileGetVersion(A_ScriptDir "\CWI SB\CWI Settings.exe")
			IniWrite, % FileGetVersion(A_ScriptDir "\CWI SB\CWI Settings.exe"), %OUT_FILE%, CWI Settings, Version
		else {
			MsgBox, Could not find compiled CWI Settings.exe`n`n Aborting....
			ExitApp
		}
		Sleep 50
		
		;-- SPEC FINDER --
		;=================
		if FileGetVersion(A_ScriptDir "\Spec Finder\specFinder.exe")
			IniWrite, % FileGetVersion(A_ScriptDir "\Spec Finder\specFinder.exe"), %OUT_FILE%, Spec Finder, Version
		else {
			MsgBox, Could not find compiled specFinder.exe`n`n Aborting....
			ExitApp
		}
		Sleep 50
			
		;-- THREADS --
		;=============
		if FileGetVersion(A_ScriptDir "\Threads\Threads.exe")
			IniWrite, % FileGetVersion(A_ScriptDir "\Threads\Threads.exe"), %OUT_FILE%, Threads, Version
		else {
			MsgBox, Could not find compiled Threads.exe`n`n Aborting....
			ExitApp
		}
		Sleep 50
		
		;-- TITLER --
		;============
		if FileGetVersion(A_ScriptDir "\Titler\CWI Title Creator.exe")
			IniWrite, % FileGetVersion(A_ScriptDir "\Titler\CWI Title Creator.exe"), %OUT_FILE%, CWI Title Creator, Version
		else {
			MsgBox, Could not find compiled CWI Title Creator.exe`n`n Aborting....
			ExitApp
		}
		Sleep 50

				
				



;===================================================================================================
; Function: param
;
;		Handles parameters passed as argument strings & returns an array containing all parameter
;       switches and their values. 
;
;       Finds all key parameters marked with a dash (-) or slash (/), such as
;
;           > -path {some path}     OR
;           > /path {some path}
;
;		and assigns the array value {Object}.{Parameter Name} := {Parameter value}
;
; Parameters:
;		paramString - String of parameter arguments
;
; Returns:
;		Array of parameter values.
;
; Remarks:
;		If a key is found with no corresponding value, such as the key "run" in this example:
;				-run -move <path>
;		then the function will set it's value in the return array to 1. In the example, p.run = 1 (true)
;
; Examples:
;   (Start code)
;		paramList = -file "c:\windows\notepad.exe" -time 20
;
;       ;Call the function, passing the parameter string
;		p := params(paramList)
;
;       ;Display the values assigned to the object array
;		msgbox, % p.file
;		msgbox, % time.file
;
;		*Output*
;
;		Msgbox 1-
;
;			c:\windows\notepad.exe
;
;		Msgbox 2-
;
;			20
;   (End)
;===================================================================================================
param(p) {
    p := RegExReplace(p , "([^\s])-", "$1<dash>")   ; change - to <dash> to avoid confusion 
    regex = (?<=[-|/])([a-zA-Z0-9]*)[ |:|=|"]*([\w|.|@|?|#|$|`%|=|*|,|<|>|^|'|{|}|\[|\]|;|(|)|_|&|+| |:|!|~|/|\\]*)["| ]*(.*)
    
    count:=0
    options:=Object()
    
    while p != "" 
    {
        count++
        
        RegExMatch(p,regex,data) 
        
        name := data1
        value := data2
        value := RegExReplace(value , "<dash>", "-")   ; change <dash> back to -        
                
        if (value = "") {
            options[name] := 1
        } else {
            options[name] := value
        }
        
        p := data3
    }
    ErrorLevel := count 
    Return options
}






	


;=================================================================
;                            INCLUDE FILES
;=================================================================
#Include %A_ScriptDir%\Util\_cIni.ahk
	






	
;=========================================================
;	Group: About
;		General script information
;
;		- *Written By:* 	Rameen Bakhtiary
;		- *Last Modified:* 	12/7/2011
;=========================================================



;____________________________________ E N D   O F   S C R I P T ____________________________________
;%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%
Sleep 100
ExitApp


